<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      <el-form-item label="姓名" prop="empName">
        <el-input
          v-model="queryParams.empName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="工号" prop="empNo">
        <el-input
          v-model="queryParams.empNo"
          placeholder="请输入工号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="职位" prop="posId">
        <el-input
          v-model="queryParams.posId"
          placeholder="请输入职位"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="所属部门" prop="depId">
        <!-- <el-select
          v-model="queryParams.depId"
          placeholder="请选择所属部门"
          clearable
          class="!w-240px"
        >
          <el-option label="请选择字典生成" value="" />
        </el-select> -->
        <el-tree-select
          v-model="queryParams.depId"
          :data="deptTree"
          :props="defaultProps"
          check-strictly
          default-expand-all
          placeholder="请选择上级部门"
          value-key="deptId"
          clearable
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="职位序列" prop="seqId">
        <el-select
          v-model="queryParams.seqId"
          placeholder="请选择职位序列"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in list2"
            :key="dict.id"
            :label="dict.seqName"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="入职时间" prop="collegeEntryDate">
        <el-date-picker
          v-model="queryParams.collegeEntryDate"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="身份标记" prop="typeFlag">
        <el-select
          v-model="queryParams.typeFlag"
          placeholder="请选择身份标记"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.TYPE_FLAG)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态标记" prop="statusFlag">
        <el-select
          v-model="queryParams.statusFlag"
          placeholder="请选择状态标记"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getStrDictOptions(DICT_TYPE.STATUS_FLAG)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['pm:employee-info:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['pm:employee-info:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :stripe="true"
      :show-overflow-tooltip="true"
      highlight-current-row
      @current-change="handleCurrentChange"
      fit
    >
      <el-table-column label="主键ID" align="center" prop="id" />
      <el-table-column label="所属部门" align="center" prop="depId" width="130px">
        <template #default="scope">
          <div v-for="item in list3" :key="item.id">
            <span v-if="item.id === scope.row.depId">{{ item.name }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="职位序列" align="center" prop="seqId" >
        <template #default="scope">
          <div v-for="item in list2" :key="item.id">
            <span v-if="item.id === scope.row.seqId">{{ item.seqName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="职位" align="center" prop="posId" >
        <template #default="scope">
          <div v-for="item in list4" :key="item.id">
            <span v-if="item.id === scope.row.posId">{{ item.name }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="empType" />
      <el-table-column label="姓名" align="center" prop="empName" />
      <el-table-column label="工号" align="center" prop="empNo" />
      <el-table-column label="手机" align="center" prop="mobile" />
      <el-table-column label="性别" align="center" prop="gender" />
      <el-table-column label="生日" align="center" prop="birthDate" :formatter="dateFormatter2" />
      <el-table-column label="身份证号" align="center" prop="idNo" />
      <el-table-column label="政治面貌" align="center" prop="politicalStatus" />
      <el-table-column label="民族" align="center" prop="ethnicity" />
      <el-table-column label="婚姻状况" align="center" prop="maritalStatus" />
      <el-table-column label="籍贯" align="center" prop="hometown" />
      <el-table-column label="英语等级" align="center" prop="englishLevel" />
      <el-table-column label="校外工作起始日期" align="center" prop="externalDate" :formatter="dateFormatter2" width="140px"/>
      <el-table-column label="学院入职时间" align="center" prop="collegeEntryDate" :formatter="dateFormatter2" width="130px"/>
      <el-table-column label="身份证地址" align="center" prop="idAddress" width="100px"/>
      <el-table-column label="现家庭地址" align="center" prop="homeAddress" width="100px"/>
      <el-table-column label="教师资格证" align="center" prop="teacherCert" width="100px"/>
      <el-table-column label="资格证编号" align="center" prop="teacherCertNo" width="100px"/>
      <el-table-column label="银行卡号" align="center" prop="bankCardNumber" />
      <el-table-column label="紧急联系人" align="center" prop="emergencyContact" width="100px"/>
      <el-table-column label="与紧急联系人关系" align="center" prop="contactRelationship" />
      <el-table-column label="紧急联系人电话" align="center" prop="emergencyPhone" />
      <el-table-column label="身份标记" align="center" prop="typeFlag" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TYPE_FLAG" :value="scope.row.typeFlag" />
        </template>
      </el-table-column>
      <el-table-column label="状态标记" align="center" prop="statusFlag" >
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.STATUS_FLAG" :value="scope.row.statusFlag" />
        </template>
      </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter2"
        width="150px"
      />
      <el-table-column label="备注信息" align="center" prop="remarks" />
      <el-table-column label="操作" align="center" min-width="120px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['pm:employee-info:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['pm:employee-info:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <EmployeeInfoForm ref="formRef" @success="getList" />
  <!-- 子表的列表 -->
  <ContentWrap>
    <el-tabs model-value="employeeEducations">
      <el-tab-pane label="职工学历" name="employeeEducations">
        <EmployeeEducationsList :emp-id="currentRow.id" />
      </el-tab-pane>
      <el-tab-pane label="职工职称" name="employeeTitles">
        <EmployeeTitlesList :emp-id="currentRow.id" />
      </el-tab-pane>
    </el-tabs>
  </ContentWrap>
</template>

<script setup lang="ts">
import { getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter, dateFormatter2 } from '@/utils/formatTime'
import download from '@/utils/download'
import { EmployeeInfoApi, EmployeeInfoVO } from '@/api/pm/employeeinfo'
import EmployeeInfoForm from './EmployeeInfoForm.vue'
import EmployeeEducationsList from './components/EmployeeEducationsList.vue'
import EmployeeTitlesList from './components/EmployeeTitlesList.vue'
import { PositionsSeqApi, PositionsSeqVO } from '@/api/pm/positionsseq'//1
import * as DeptApi from '@/api/system/dept'
import { defaultProps, handleTree } from '@/utils/tree'
import * as PostApi from '@/api/system/post'

/** 职工信息 列表 */
defineOptions({ name: 'EmployeeInfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<EmployeeInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  depId: undefined,
  seqId: undefined,
  posId: undefined,
  empType: undefined,
  empName: undefined,
  empNo: undefined,
  mobile: undefined,
  gender: undefined,
  idNo: undefined,
  collegeEntryDate: [],
  typeFlag: undefined,
  statusFlag: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await EmployeeInfoApi.getEmployeeInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
    console.log(list.value)
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await EmployeeInfoApi.deleteEmployeeInfo(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await EmployeeInfoApi.exportEmployeeInfo(queryParams)
    download.excel(data, '职工信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 选中行操作 */
const currentRow = ref({}) // 选中行
const handleCurrentChange = (row) => {
  currentRow.value = row
}

/** 初始化 **/
onMounted(() => {
  getList()
  getList2()
  getList3()
  getList4()
  getTree()
})

const queryParams2 = reactive({
  pageNo: 1,
  pageSize: 10,
  seqName: undefined,
  seqDec: undefined,
  createTime: [],
  remarks: undefined,
})
const list2 = ref<PositionsSeqVO[]>([]) // 列表的数据
/** 查询列表 */
const getList2 = async () => {
  try {
    const data = await PositionsSeqApi.getPositionsSeqPage(queryParams2)
    list2.value = data.list
  } finally {
  }
}

const queryParams3 = reactive({
  pageNo: 1,
  pageSize: 100,
  name: undefined,
  status: undefined
})
const list3 = ref() // 列表的数据
/** 查询部门列表 */
const getList3 = async () => {
  try {
    const data = await DeptApi.getDeptPage(queryParams3)
    list3.value = data
    console.log("111",list3.value)
  } finally {
  }
}

const deptTree = ref() // 树形结构
/** 获得部门树 */
const getTree = async () => {
  deptTree.value = []
  const data = await DeptApi.getSimpleDeptList()
  let dept: Tree = { id: 0, name: '顶级部门', children: [] }
  dept.children = handleTree(data)
  deptTree.value.push(dept)
}

const list4 = ref() // 列表的数据
const queryParams4 = reactive({
  pageNo: 1,
  pageSize: 10,
  code: '',
  name: '',
  status: undefined
})
/** 查询岗位列表 */
const getList4 = async () => {
  try {
    const data = await PostApi.getPostPage(queryParams4)
    list4.value = data.list
  } finally {
  }
}
</script>